﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace YaP.Library.YapControls
{
    public partial class MainFormConfigurator : UserControl
    {
        private Color colorField;
        private string FileName;
        public MainFormConfigurator()
        {
            InitializeComponent();
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK != colorDialog1.ShowDialog()) return;
            colorField = colorDialog1.Color;
            textBox1.Text = colorDialog1.Color.ToString();
            if (OnMainBackColorChanged != null)
                OnMainBackColorChanged(this, colorField);
        }

        public delegate void MainBackColorChanged(object sender, Color c);

        public delegate void BackImgChanged(object sender, string FilePath);

        public delegate void ImgModeChanged(object sender, ImageLayout layout);

        public event MainBackColorChanged OnMainBackColorChanged;
        public event BackImgChanged OnImgChanged;
        public event ImgModeChanged OnImgModeChanged;

        private void textBox2_Click(object sender, EventArgs e)
        {
            Thread ImgModder = new Thread(() =>
                                              {
                                                  if (DialogResult.OK != openFileDialog1.ShowDialog()) return;
                                                  if (textBox2.InvokeRequired)
                                                      textBox2.BeginInvoke(
                                                          (MethodInvoker)
                                                          (() => textBox2.Text = openFileDialog1.SafeFileName));

                                                  FileName = openFileDialog1.FileName;
                                                  if (OnImgChanged != null)
                                                      OnImgChanged(this, FileName);
                                              });

            ImgModder.SetApartmentState(ApartmentState.STA);
            ImgModder.Start();

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem.ToString() == "Tile")
            {
                if (OnImgModeChanged != null)
                    OnImgModeChanged(this, ImageLayout.Tile);
            }
            else
                if (OnImgModeChanged != null)
                    OnImgModeChanged(this, ImageLayout.Stretch);
        }
    }
}
